package com.example.word_book.service;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.word_book.R;
import com.example.word_book.bean.Word;
import com.example.word_book.utils.FragmentHelp;
import com.example.word_book.utils.SqlHelp;

import java.util.ArrayList;

public class DayShorthandActivity extends AppCompatActivity {

    private boolean flag = false;
    private Word word;
    private ArrayList<Word> wordArrayList = new ArrayList<>();
    private int location = 0;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_shorthand);

        init();
    }

    private void init() {

        SqlHelp sqlHelp = new SqlHelp(this);
        ImageView back = findViewById(R.id.img_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Button btn_last = findViewById(R.id.btn_last);
        Button btn_next = findViewById(R.id.btn_next);
        word = sqlHelp.selectWord(sqlHelp.randomWord());
        wordArrayList.add(word);
//        Log.i(word.getWord(), "当前");
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        FragmentHelp fragment = new FragmentHelp(wordArrayList.get(location));
        fragmentTransaction.replace(R.id.fv, fragment);
        fragmentTransaction.commit();
        btn_last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (location > 0) {
                    location--;
                    fragmentManager = getSupportFragmentManager();
                    fragmentTransaction = fragmentManager.beginTransaction();
                    FragmentHelp fragment = new FragmentHelp(wordArrayList.get(location));
                    fragmentTransaction.replace(R.id.fv, fragment);
                    fragmentTransaction.commit();
                }else {
                    Toast.makeText(DayShorthandActivity.this, "没有上一个单词了", Toast.LENGTH_SHORT).show();
                }

            }
        });

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (location == wordArrayList.size() - 1) {
                    word = sqlHelp.selectWord(sqlHelp.randomWord());
                    wordArrayList.add(word);
                }
                location++;
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                FragmentHelp fragment = new FragmentHelp(wordArrayList.get(location));
                fragmentTransaction.replace(R.id.fv, fragment);
                fragmentTransaction.commit();
            }
        });
    }
}